//
//  KDGetVpnServiceTool.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/12/9.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDGetVpnServiceTool.h"
#import "KDVpnResult.h"
#import "KDBaseTool.h"
#import "KDVpnModel.h"
#import "KDBaseParam.h"
#import "KDVpnDataTool.h"
#import "KDGetVpnServiceParam.h"
#import "KDTabBarController.h"

#define LAST_REFRESH_VPNLIST_TIME @"lastRefreshVpnListTime"//上一次更新vpn列表的时间

@interface KDGetVpnServiceTool ()
@property (nonatomic, retain) NSDateFormatter *fmt;
@property (nonatomic, strong) NSArray *infoArr;
@property (nonatomic, strong) KDVpnModel *model;
@end
@implementation KDGetVpnServiceTool

+ (void)getVpnServiceWithParam:(KDGetVpnServiceParam *)param success:(void (^)(KDVpnResult *responseObejct))success failure:(void(^)(NSError *error))failure{
    NSString *url =  [NSString stringWithFormat:@"%@%@",AuthServerAddr,@"vpn/login"];
    [KDBaseTool post:url param:param resultClass:[KDVpnResult class] success:success failure:failure];
}

+ (void)aliveVpnOnlineStatusWithParam:(KDVpnOnlineStatusParam *)param success:(void (^)(KDStatusResult *))success failure:(void (^)(NSError *))failure{
    NSString *url =  [NSString stringWithFormat:@"%@%@",AuthServerAddr,@"vpn/vpnOnlineStatus"];
    [KDBaseTool post:url param:param resultClass:[KDVpnResult class] success:success failure:failure];

}

- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"yyyy-MM-dd";
    }
    return _fmt;
}
/**
 *  日期相差多少天
 */
- (NSInteger)leftDayfromDate:(NSDate *)today toDay:(NSDate *)lastDay{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitDay;//年、月、日、时、分、秒、周等等都可以
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:lastDay toDate:today options:0];
    KDLog(@"%ld", [comps day]);
    return [comps day];
}
/**
 *  获取vpn
 */
- (void)getVpnModel
{
//    [self class] getVpnModelSuccess:^(KDVpnModel *model) {
//        
//    }
}

- (void)postNotyWithModel:(KDVpnModel *)model{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TabbarCtrlGetVpnModelNoty" object:nil userInfo:@{@"KDVpnModel":model}];
    });
}
+ (void)getVpnModel{
    KDAccount *account = [KDAccountTool account];
    KDGetVpnServiceParam *param = [[KDGetVpnServiceParam alloc]init];
    param.loginType = 0;
    param.loginId = account.token;
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
    param.ts = timeInterval;
    param.au = @"1";
    [self getVpnServiceWithParam:param success:^(KDVpnResult *responseObejct) {
        if ([responseObejct.msg isEqualToString:@"操作成功"]) {
            //获取一个随机的vpn服务器
            if (responseObejct.data.count) {
                
                KDAccount *account = [KDAccountTool account];
                
                account.gid = responseObejct.gid;
                
                [KDAccountTool saveAccount:account];
                
                KDVpnModel *model = responseObejct.data[0];
//                success(model);
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginPageGetVpnModelNoty" object:nil userInfo:@{@"KDVpnModel":model}];
                [KDVpnDataTool saveVpnList:responseObejct.data];
            }
           
        }else if ([responseObejct.msg isEqualToString:@"Token已失效"]){
            
//            NSArray *list = [KDVpnDataTool Vpnlist];
//            if (list.count) {//如果本地有缓存，
//                KDVpnModel *model = list[arc4random()%(list.count)];
////                success(model);
//                 [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginPageGetVpnModelNoty" object:nil userInfo:@{@"KDVpnModel":model}];
//            }else{
                KDNewfeatureController *new = [[KDNewfeatureController alloc]init];
                __block KDTabBarController*ctr = (KDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                [UIApplication sharedApplication].keyWindow.rootViewController = new;
                [ctr removeObserver];
                [ctr removeFromParentViewController];
                ctr = nil;
            
        }
    } failure:^(NSError *error) {
        NSArray *list = [KDVpnDataTool Vpnlist];
        if (list.count) {
            KDVpnModel *model = list[arc4random()%(list.count)];
            //                success(model);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginPageGetVpnModelNoty" object:nil userInfo:@{@"KDVpnModel":model}];
        }
    }];
}
@end
